package com.demo.generate;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.xml.*;

import java.util.Iterator;
import java.util.List;

/**
 * mybatis generator 增强插件，提供动态sql查询
 */
public class DynamicSqlPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        XmlElement rootElement = document.getRootElement();

        // 生成新的 getList select 元素
        XmlElement getListElement = new XmlElement("select");
        getListElement.addAttribute(new Attribute("id", "getList"));
        getListElement.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        getListElement.addElement(new TextElement("select"));
        getListElement.addElement(new TextElement("  <include refid=\"Base_Column_List\" />"));
        getListElement.addElement(new TextElement("from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));

        // 生成新的 getOne select 元素
        XmlElement getOneElement = new XmlElement("select");
        getOneElement.addAttribute(new Attribute("id", "getOne"));
        getOneElement.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        getOneElement.addElement(new TextElement("select"));
        getOneElement.addElement(new TextElement("  <include refid=\"Base_Column_List\" />"));
        getOneElement.addElement(new TextElement("from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));

        // 使用 ElementRenderer 生成 where 元素的内容
        for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
            // 生成 getList 的条件
            XmlElement ifElementGetList = new XmlElement("if");
            ifElementGetList.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty() + " != null and " +
                    introspectedColumn.getJavaProperty() + " != ''"));
            ifElementGetList.addElement(new TextElement(introspectedColumn.getActualColumnName() + " = #{" + introspectedColumn.getJavaProperty() + "}"));
            getListElement.addElement(ifElementGetList);

            // 生成 getOne 的条件
            XmlElement ifElementGetOne = new XmlElement("if");
            ifElementGetOne.addAttribute(new Attribute("test", introspectedColumn.getJavaProperty() + " != null and " +
                    introspectedColumn.getJavaProperty() + " != ''"));
            ifElementGetOne.addElement(new TextElement(introspectedColumn.getActualColumnName() + " = #{" + introspectedColumn.getJavaProperty() + "}"));
            getOneElement.addElement(ifElementGetOne);
        }

        // 将新生成的getList和getOne元素添加到rootElement中
        rootElement.addElement(getListElement);
        rootElement.addElement(getOneElement);

        // 移除已经生成的Example相关的元素，避免冲突
        Iterator<VisitableElement> iterator = rootElement.getElements().iterator();
        while (iterator.hasNext()) {
            VisitableElement element = iterator.next();
            if (element instanceof XmlElement) {
                XmlElement xmlElement = (XmlElement) element;
                if (xmlElement.getName().contains("ByExample")) {
                    iterator.remove();
                }
            }
        }

        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }


    @Override
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles(IntrospectedTable introspectedTable) {
        // 移除已经生成的Example相关的XML文件，避免冲突
        List<GeneratedXmlFile> generatedXmlFiles = super.contextGenerateAdditionalXmlFiles(introspectedTable);

        // 移除与Example相关的XML文件
        Iterator<GeneratedXmlFile> iterator = generatedXmlFiles.iterator();
        while (iterator.hasNext()) {
            GeneratedXmlFile generatedXmlFile = iterator.next();
            if (generatedXmlFile.getFileName().contains("Example")) {
                iterator.remove();
            }
        }

        return generatedXmlFiles;
    }


}

